from sqlalchemy import Column, DateTime, Float, Integer, String, Text, func, text

from databases.sync import Base


class CodeModel(Base):
    __tablename__ = "code"
    __table_args__ = {
        "comment": "秘钥",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(50), nullable=False)
    prefix = Column(String(32), nullable=False)
    code_id = Column(Integer, nullable=False)
    status = Column(Integer, nullable=True, server_default=text("1"))
    created_time = Column(DateTime, nullable=True, default=func.now())
    draw_time = Column(DateTime, nullable=True, default=func.now())
    activity_id = Column(Integer, nullable=True)
    prize_id = Column(Integer, nullable=True)
    prize_index = Column(Integer, nullable=True)
    code_batch_id = Column(Integer, nullable=True)


class CodeBatchModel(Base):
    __tablename__ = "code_batch"

    id = Column(Integer, primary_key=True, autoincrement=True)
    prefix = Column(String(32), nullable=True, comment="前缀")
    begin_num = Column(Integer, nullable=False, default=0)
    end_num = Column(Integer, nullable=False, default=0)
    status = Column(Integer, nullable=False, default=0)
    remark = Column(Text, nullable=True)
    export_num = Column(Integer, nullable=False, default=0)
    created_time = Column(DateTime, nullable=True, default=func.now())


class PrizeModel(Base):
    __tablename__ = "prize"
    __table_args__ = {
        "comment": "奖品",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), nullable=False)
    short_name = Column(String(20), nullable=False)
    img = Column(String(2048), nullable=False)
    num = Column(Integer, nullable=True)
    status = Column(Integer, nullable=True, server_default=text("1"))


class ActivityModel(Base):
    __tablename__ = "activity"

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(50), nullable=False, comment="活动标题")
    merchant_id = Column(Integer, nullable=True, comment="商家id")
    num = Column(Integer, nullable=True, comment="活动名额")
    draw_id = Column(Integer, nullable=True, server_default=text("1"))
    start_time = Column(DateTime, nullable=True, comment="活动开始时间")
    end_time = Column(DateTime, nullable=True, comment="活动结束时间")
    cancel_start_time = Column(DateTime, nullable=True, comment="核销开始时间")
    cancel_end_time = Column(DateTime, nullable=True, comment="核销结束时间")
    is_bind_client_phone = Column(
        Integer, nullable=True, comment="是否绑定手机号", server_default=text("1")
    )
    is_day_limit_num = Column(
        Integer, nullable=True, comment="是否限制每日领取数量", server_default=text("1")
    )
    day_limit_num = Column(Integer, nullable=True, comment="每日限制领取数量")
    prefix = Column(String(32), nullable=True, comment="前缀")
    start_code_id = Column(Integer, nullable=True, comment="开始码id")
    end_code_id = Column(Integer, nullable=True, comment="结束码id")
    status = Column(Integer, nullable=True, comment="状态",
                    server_default=text("0"))
    kefu_img = Column(String(2048), nullable=True, comment="客服图片")
    remark = Column(Text, nullable=True, comment="备注")
    rules_desc = Column(Text, nullable=True, comment="规则描述")
    created_time = Column(DateTime, default=func.now(), comment="创建时间")
    updated_time = Column(
        DateTime, default=func.now(), onupdate=func.now(), comment="更新时间"
    )
    deleted_time = Column(DateTime, comment="删除时间")
    created_by = Column(String(32), comment="创建者")
    updated_by = Column(String(32), comment="更新者")
    deleted_by = Column(String(32), comment="删除者")
    is_deleted = Column(Integer, server_default=text("0"), comment="是否删除")


class ActivityPrizeModel(Base):
    __tablename__ = "activity_prize"
    __table_args__ = {
        "comment": "活动奖品中间表",
        "mysql_engine": "MyISAM",
        "mysql_collate": "utf8_general_ci",
    }

    id = Column(Integer, primary_key=True, autoincrement=True)
    prize_id = Column(Integer, nullable=True)
    activity_id = Column(Integer, nullable=True)
    num = Column(Integer, nullable=True)
    prize_level_id = Column(String(32), nullable=True, comment="奖品等级id")
    probability = Column(Float(12), nullable=True, comment="概率")
    prize_index = Column(Integer, nullable=True)


class DrawModel(Base):
    __tablename__ = "draw"

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(32), nullable=True, comment="标题")
    prize_num = Column(Integer, nullable=True, comment="奖品数量")
    imgs = Column(Text, nullable=True, comment="图片列表")
    img_head = Column(String(50), nullable=True, comment="头图")
    path = Column(String(50), nullable=True, comment="页面路径")
    status = Column(Integer, nullable=True, comment="状态",
                    server_default=text("1"))
    remark = Column(Text, nullable=True, comment="备注")
    desc = Column(Text, nullable=True, comment="描述")


class ActivityOrderModel(Base):
    __tablename__ = "activity_order"
    id = Column(Integer, primary_key=True, autoincrement=True)
    contact_name = Column(String(50), nullable=True)
    contact_phone = Column(String(50), nullable=True)
    contact_address = Column(Text, nullable=True)
    prize_id = Column(Integer, nullable=True)
    prize = Column(String(50), nullable=True)
    prize_level_id = Column(Integer, nullable=True)
    prize_level = Column(String(50), nullable=True)
    merchant = Column(String(50), nullable=True)
    client_id = Column(Integer, nullable=True)
    activity = Column(String(50), nullable=True)
    activity_id = Column(Integer, nullable=True)
    code = Column(String(50), nullable=True)
    status = Column(Integer, nullable=True)
    created_time = Column(DateTime, default=func.now())
    remark = Column(Text, nullable=True)
